package com.lzhl.buildjar.dao.update;

import com.lzhl.buildjar.dao.BaseDao;

import java.sql.SQLException;
import java.util.UUID;

/**
 * 数据版本1更新脚本
 * @author 李柱良(@柱子的日月)
 * @date 2014/6/18
 */
public class Update_1 extends BaseDao implements Updater {

    @Override
    public void update() throws SQLException {
        createVersionTable();
        createCategoryTable();
        createSchemeTable();
        createSettingTable();
    }

    /**
     * 创建版本表
     * @throws java.sql.SQLException
     */
    private void createVersionTable() throws SQLException {
        StringBuffer sql = new StringBuffer();

        sql.append("CREATE TABLE IF NOT EXISTS T_Version ( \n");
        sql.append("    FVersion INT \n");
        sql.append(") \n");
        executeUpdate(sql.toString());

        if(!isTableHasData("T_Version")) {
            sql.setLength(0);
            sql.append("INSERT INTO T_Version (FVersion) VALUES (0) \n");
            executeUpdate(sql.toString());
        }
    }

    private void createCategoryTable() throws SQLException {
        StringBuffer sql = new StringBuffer();

        sql.append("CREATE TABLE IF NOT EXISTS T_Category ( \n");
        sql.append("    id INTEGER PRIMARY KEY AUTOINCREMENT, \n");
        sql.append("    FParentID INTEGER, \n");
        sql.append("    FSeq INT, \n");
        sql.append("    FName TEXT \n");
        sql.append(") \n");

        executeUpdate(sql.toString());
    }

    private void createSchemeTable() throws SQLException {
        StringBuffer sql = new StringBuffer();

        sql.append("CREATE TABLE IF NOT EXISTS T_Scheme ( \n");
        sql.append("    id INTEGER PRIMARY KEY AUTOINCREMENT, \n");
        sql.append("    FCategoryID INTEGER, \n");
        sql.append("    FName TEXT, \n");
        sql.append("    FSeq INT, \n");

        sql.append("    FClasspath TEXT, \n");
        sql.append("    FOutput TEXT, \n");
        sql.append("    FManifest TEXT, \n");
        sql.append("    FMainclass TEXT, \n");
        sql.append("    FOpNote INT, \n");
        sql.append("    FNote TEXT, \n");
        sql.append("    FClassitems TEXT, \n");

        sql.append("    FCreateTime TIMESTAMP, \n");
        sql.append("    FUpdateTime TIMESTAMP, \n");
        sql.append("    FOutputTime TIMESTAMP \n");
        sql.append(") \n");

        executeUpdate(sql.toString());
    }

    private void createSettingTable() throws SQLException {
        StringBuffer sql = new StringBuffer();

        sql.append("CREATE TABLE IF NOT EXISTS T_Setting ( \n");
        sql.append("    FKey TEXT PRIMARY KEY, \n");
        sql.append("    FValue TEXT \n");
        sql.append(") \n");

        executeUpdate(sql.toString());
    }

    public static void main(String[] args) {
        System.out.println(UUID.randomUUID());
    }

}
